﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.BeamNG.VehiclePartConfig;
using JBeamLibrary.Tools;
using JBeamLibrary.VehicleParts.Elements;
using JBeamLibrary.VehicleParts.Enums;

namespace DriveBeamEditor.Controls.BeamNG.SectionControls
{
    public partial class DifferentialsControl : VehiclePartControl
    {
        #region CTORs

        public DifferentialsControl()
        {
            InitializeComponent();
            InitGridBindings();
        }

        #endregion

        #region Events

        #region Binding source

        private void differentialsBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            if (DesignMode || VehiclePart == null)
            {
                e.NewObject = new Differential();
                return;
            }

            /* Avoid loop */
            if (e.NewObject is Differential) return;

            /* Create a new hydro */
            e.NewObject = new Differential(Randomizer.GetRandomName(), Randomizer.GetRandomName())
            {
                ClosedTorque = 0,
                EngineTorqueCoef = 0f,
                Type = DifferentialType.Open,
                State = DifferentialType.Open
            };
        }

        private void differentialsBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        #endregion

        #region Data grid view

        private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            grid_CellContentClick(sender, e);
        }

        private void grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            /* Clicks on headers */
            if (e.ColumnIndex < 0 || e.RowIndex < 0) return;

            /* Get column & row & cell */
            var col = grid.Columns[e.ColumnIndex];
            var row = grid.Rows[e.RowIndex];

            /* Validate new row */
            if (row.IsNewRow && col == columnDelete)
            {
                differentialsBindingSource.EndEdit();
                grid.NotifyCurrentCellDirty(true);
                grid.EndEdit();
                grid.NotifyCurrentCellDirty(false);
            }

            /* Delete the current row */
            else if (col == columnDelete)
            {
                grid.Rows.RemoveAt(e.RowIndex);
            }
        }

        #endregion

        #region Popups

        #endregion

        #endregion

        #region Methods

        public override string ToString()
        {
            return "Differentials";
        }

        private void InitGridBindings()
        {
            columnType.DataSource = Enum.GetValues(typeof(DifferentialType));
            columnState.DataSource = Enum.GetValues(typeof(DifferentialType));
        }

        private void BindDifferentials()
        {
            grid.Enabled = false;
            differentialsBindingSource.DataSource = null;

            if (VehiclePart != null)
                differentialsBindingSource.DataSource = VehiclePart.Differentials;

            grid.Refresh();
            grid.Enabled = true;
        }

        protected override void OnVehiclePartChanged()
        {
            BindDifferentials();
            base.OnVehiclePartChanged();
        }

        #endregion

    }
}
